/**
 * Created by timxuan on 2016/11/10.
 */
/* eslint-disable */

import * as $ from "../tool";
import utilWeb from "../utilWeb";
// import lowDebug from 'lowDebug';

let win = window,
    hostname = location.hostname,
    // boolKg = (window.external && window.external.superCall) || ua.match(/KGBrowser/gi),
    boolFX = $.boolFX,
    sign =
        "eNRH8z3HykKmEPLuEX66rurSdDsI7NlGsWNhg+Sk9jt5490HIqXPaoo/rj0ikFKBJ1Bqi6x83u2URe4jMTMh98An3MfdSKU/ffmPJZL0a6Irkrzouq7R2iIgprYBBVihfXNQxtCp2p8R6MJVZoovb7qhqfGJlYvo4S6ZM5VIYKg=";

if (!hostname.match("mfanxing.kugou.com")) {
    if (hostname.match("mfanxing2.kugou.com")) {
        sign =
            "q10lncZ2yCB+FjH+GlykO8H1+mDO6OVja4p71l7VfNBjJmu6FSw1izO54r4aVR2PcEJ0b23ZZbt7yYhIoVpg0QcY20NyAzElZUWz59pAGQq0GPksXttXRJeS+pdfhPVxrnkZUA6cj/Qyg5Yc+pVIwH+VgnX+Mvzv2F69Ah8ozHg=";
    } else if (hostname.match("fx.service.kugou.com")) {
        sign =
            "QNFo3SMm+8DSMIJShGSPJZz68k4ePVawnG4CbbeRospZKcKjBL2cQhWgoaTOy81TUGl/A04IrRREALY+TREyBweD9f4n5lilpl+EeFlWz5wyJ+vslEA6G6r9Td7Hp7VCwpMWijuLuqNCh0CFrY9iOhRThf09SEFcCXMKbk0DlBY=";
    } else if (hostname.match("mfortest.fxwork.kugou.com")) {
        sign =
            "5BDR3+U56HsowM8Dz6xsXt5IB7YORuM2al0VBgae1Rjw8GA3uaskY0d5Jx85+DlxensStnDMRhafQLuYLNluDw2FmtabY+EZ/5TwzNEMBst7GAHYtByckpPWUCoavc5V4M/cUFYObE3q1qxDNt/SEGF3nvqUQ3tobBHZuCSGhpw=";
    } else if (hostname.match("mforrel.fxwork.kugou.com")) {
        sign =
            "wV2vahk4Ru8iPHwP2rKOCfVXyWuPkPCTZi3d1HXXSqhyyWvy99T3C2iAmn33UEcvTKjrvQf0sCkA8sSBzqIVo/g3JOYgGqevBnflyEogL1sowU8eXN7QAgMQiYqqQRHRSQW+fnAcLz595NpDjwKFjj88bE/QHs8Mkky3caVYXxs=";
    } else if (hostname.match("mfx.kugou.com")) {
        sign =
            "FbXefqZJe3PGQJvd8T9jMdrlLniC2vu2vSb7rQHLXkVZD5JjyvsiQQeZE/Es9UhJC6VrR/BVn87ucnLx2Kxu3+Gqs3NREBT2SMRZXF7MTA3YsNlBiAFD0WPwZkEEC3SMFBJwjjGTOVGT4o9jT82w3kIu2RHLWz50Qzb/F5a0XZk=";
    } else if (hostname.match("kflcgmobile.fxwork.kugou.com")) {
        sign =
            "YrJugZdlG2mMIvm8/0Lu9dJnC+yk7hVv7diKyQWsUY+zQKWOirlWIANlXBxoCTlCo+VHOADD6xX21igJ3TMcrSOyvd8jrPvaw+7hkhLdjzsqrR+cLZn4DjqpLHSkHbtih58gy1tv2Y35gNI/ZGoonmCgaKR25wDz0nBtLDOJA+E=";
    } else if (hostname.match("kfcwcmobile.fxwork.kugou.com")) {
        sign =
            "ljWodupDQneOTbtfpv9CYDfW2T0hAxO/RDV0x45FbCQ8WpLnAlQsc/ofnBvD/fZXqMZvdL/8bm4iiNT4CGnvcxtMwojyRV3C3gM3ScOvMghIe355TZxSF9swf5uJeMRDmxDd0ly7MDGYdyXSXB919Q53GJRcDsbYCuF/YlmmxaQ=";
    } else if (hostname.match("kffymmobile.fxwork.kugou.com")) {
        sign =
            "HdwVD84rw2wmWufaciyzZsKhfN/Uud3SCUksgHLTNr1L4xSWnDtaXewjanpgkSgOMCS0zqsWh82ylTTDRfzRpRt/lVaBM2Ye6lJAG7Tb87C7zOqZvnkHblUmpH6iczALEQZXrtfU2mNyQyyQ0JoLLCylmWRYLPkhLW0nPDrwmPs=";
    } else if (hostname.match("nmfx94.fxwork.kugou.com")) {
        sign =
            "BeDBBPGLyTWb++1afNeRmm9r7q3rDVJlPfe1yjaEADqU1jOrS8bSmrHOo2zJmPgHiinaN+47YkuOM5JveAGcJYy2Ho6MnXZa7FlxyDI17fPKn21vlPpxPEqvNhlqwPAsY6l4i2t+zOpSynwCAVMoV8sqz1joj7dq1lUufYSxGzQ=";
    } else if (hostname.match("kfcjcnmobile.fxwork.kugou.com")) {
        sign =
            "WX2WRUYli8iBETuteMaJG3to+7EPYKvX+h38XIpSNzyXPzyOqlVwiNxeh+EI+rxdZXLQ00aMBA7xdb6T860Bl6HrwSRCCf83qtSrAOkONtRRhG8HjZYY6lJKA7SsSOaygDcC+n+MCstVXZhVXOV5nhCM+2bTcaOVePBnogsONYY=";
    } else if (hostname.match("kflyjnmobile.fxwork.kugou.com")) {
        sign =
            "TXunpq44qPvwxsWj3H5rIHnqwc+7ERyAOCiubUvHC8bXzoLeFieqsttJ2H3ZNG0G3/he4XooWeJOQyt1lfp2ssivqmm1djxiA0CdeMIxgufGzlwdqYR9BBbUNpf3/dYnSyhDmt8KTRcZi8oGvgVEBfp17SbBwWr+Xe8xDHw7ggM=";
    } else if (hostname.match("kffymlocal.fxwork.kugou.com")) {
        sign =
            "tfFbHyd5fUKdYIKr237HmG7HTrARZTOGKcTdUP8ck1b2hyGECe7Pg2TTjoEEYksW++FBq9akSBu4J1mw8HuChnvweQbiJvoHVg0ftDVLL0x6gvp24bpuC4cDwn4Tp43PRExpd7dW0a7hnGAIQVUeV3T3bnMywFZ4zGCt28o+CWo=";
    } else if (hostname.match("mrtest.fxwork.kugou.com")) {
        sign =
            "uExjaB4TUov/QMYLreJzV6Dzk9juF9hyiamvnwNPxQ7W+D3AqD+EtZaWTfKrDULSPxx8bmULIckzsfhjYin2TFeM9IkXIuvpjgsA2ETZ+aHOTMarfwWKtiX5raCd4pLljgZw0hn7GMTKjfujec4lFwOViKzCrQfYGBJzshMnRa0=";
    } else if (hostname.match("mrtest1.fxwork.kugou.com")) {
        sign =
            "BHf49cgYDu1IuKDeDnrQlx3FyBvxvEhql52aCSnd0HPumgmNQt3li/XTvV2ZOly2tiPTfQUsUBp0msf1LN42FUqOtnm/LzdvW19TeC06w1N65iSJoctonGN54ACGdWMXRo5ZSxkzgsyaSoDOMUsXXu3FSCJBO+e2nagO1kl2gxQ=";
    } else if (hostname.match("kflyjr.fxwork.kugou.com")) {
        sign =
            "4zlPOlXG1OqjHm508NTTBFWF2vSy8bYt4y/iH1TDXWTNDaBZuSN/0awjY7XBJvD8Qz25hb0UY/ykO+CzZvuibngjSMqAQpBIyaYdk7rOdbeQcKWBtID+8J+JWRKDOco72K/FLUpRxVkd3I5wSAqOrr8mrLVo44AHUEFjZVZCBD8=";
    } else if (hostname.match("mrtest2.fxwork.kugou.com")) {
        sign =
            "SsMaaNhPPC79b4CPltfc4F3sJ9toucHy5x2CBni8LwnQ0ACrlminIVwRrkpWoe1ZFAtGE1E8rgGzAHXvCu4OVuZAVYt+VNR3cTCtL9eLcH07yB74akKp578zyt0k1/C7BkhpSOY+uS40yQ6TV11jkbz1n2y9OtDdj+Ej4qI9Wtg=";
    } else if (hostname.match("mrtest3.fxwork.kugou.com")) {
        sign =
            "1L8ZKuJs5cf34WdOjU5CRcpzesDEa379S49biuxDNMPJBCu/WbqXRM6u9sn6jfI4XEF77GtWiRClmIKOll9TBkL/QUG5MUemKx78GyM3AibbwENc36m6geR3nZgRK7xhCqQqSt+9T97GpLqarHRZaujJ9tDLT8MbgaEMG5BkgUA=";
    } else if (hostname.match("laurylu.fxwork.kugou.com")) {
        // todo
        sign =
            "apoUV2pgF5OeS6VnX2XfajV0O1iOFcREbc+MPib+qG9AgSbM1kIgLGJ+KQf3alYozXTu/r8m4CG5xRc+3SAfxWOnjtUv/o8tSgl2714V5R/PdDKqU9niDDegmjnNau3m3zTdRaOPOScmuVhzcmlFqhmANPyH17CPt2MbSu2EORc=";
    } else if (hostname.match("kffym.fxwork.kugou.com")) {
        sign =
            "pqicQwBVLp3/NNh633rydOsa1NOURIFni0DdpGfVsvmbC5HqR1IuBEOqXCPnoukdWGTHKzk82AYnMx9KG27DytqfXKKQMnPtMGWvant5wllsPbH6j15Jjz34O2RubCvKURsm3Vf+Xdp1h3fhTziXBVgI0g3dqFUmEl6NN+3XEOw=";
    }else if (hostname.match("kfzdl.fxwork.kugou.com")) {
        sign =
            "sMagqhh7+ORonDsOXIj+uh7o2tGhVuPf7rN25ZaosltffrzzFixO2fJbHd3QsdLfwe6+Eq00tz382A11YxKG5R3hKP8+DhHnRmML0BFy7IcaGFkM3EghPj/WDE/CNnllyt8xQGVdo98KBsjNiPwEze28N9EYM8GSCHyo5NtDHk0=";
    } else if(hostname.match('fx998.fxwork.kugou.com')) {
        sign = 'BMT2qd7Uy8P8mOWmXk7/cjZHszYRGzeCICHy47p6XHRNx4sKMX8Ql57YhNw0vbG9jYrHHEqUWebasRPVI8l8l0eWO8sa0dwImM8hutOWWdFaymJXywE0vIMbdQuG2HaGFlcCkShcYBzqb1+EgCh+II+wFCrY9C92Hiokxuc7F8E=';
    } else if (hostname.match("kflr.fxwork.kugou.com")) {
        sign = "d8yVQDx2APAN0NpKZ1eNtpV/3i4RqVUIU0M7hjt9mgaC+nhLOzYL3yIMnmTw+/uztef4Y+tK50wB8KhsOV5akWI/Vw7OlIOJc1bOE4xyQCCNwlJkwP2p4XVL+YB9VSlGhISu8dBIIf8LvFIi7wGb4YnszCD6+n9B+nIQBCILZbc=";
    } else if (hostname.match('kflryw.fxwork.kugou.com')) {
        sign = "4fhYzgmKoJ/+IFkocM7FkqRzZMzeyuZUTORBY2U199Dd47n34IMgXyq0OhXGm8CA2YhRDK/i31Nn8Kd1nj58AxLe6g8YNAybhuYy1suwyjbSKMh6EfQUhHNss1ZYJ7xtTEDjtHPUMVf4YDzBOeUz0NOGARNUqPPmIvLgT47GgQo=";
    } else if (hostname.match("hamyguo.fxwork.kugou.com")) {
        sign =
        "5UiZ9kkWsT2StLYy2BU5T93Ery1U52BVsirb5sHCC1Fi21R4fhvK6J3f7/JM5cGCYEhr8mtMXeG9J9NJObwqLxOXZiTYIarioWd21YmQXlXPP/sO7F3DgFZusQATxxquZtIOQfv3KIr01bvSF9xKKsWeesiYMRJ0grFsr5hrqBc=";
    } else if (hostname.match("fanxing0182.fxwork.kugou.com")){
        sign = "vyEbcE1neWaOwLJZ9HABYA3juslrlA2z+s9YoLwYpUVcAgZvnEv16xDEktdiiGF6W90yFMPkgu68GhLi7O7i/RJGfmrjLks0+2E788GeVWT6h3YDrK2qzuNvDo1nc0OyivdDc3zMtqj4ONrTm/xYh+N7EwQkO5D3rhCLzTZQ4e8=";
    } else if (hostname.match("fx0182.fxwork.kugou.com")){
        sign = "WxntyfjZI0DvnLLCF4iSgWlMMKU0x1ETSqn3hu5iVIKgNZPFL7JiUTC593vxk8E9iYjtHOq6A2QDvGd1a2YxeAu+0BBmQvjA7k2Xnsxij0TfdJ313XCtM+O+Rqro34AWD/629qh9HcdKgzx0W7BFp9u22EGTYkYh/ITaILB6Z0s=";
    } else if (hostname.match("fx0176.fxwork.kugou.com")) {
        sign = "r6yhpsgvwPU76Owa9I9f0INT334e1l3oW/SFLwg6iG8wrM3QEGGm86MiA4c/6ZjKFgeefjXKlf4xTn5rM/honwTvhs12iO3RNb35lU3Hx2Ldabw/7FJS4UutWy7VfM5+hjvirNHUq2cE9+QazfE1d2FTqn95LiaT0/KLz2NTK1g=";
    }
    //CI域名
    else if (hostname.match("ci-mreact-4611.fxwork.kugou.com")){
        sign = "oEZpRGOeHqXC4NvdOrfMaZVGgvPTN9/h52jzJ7Yih//jjVJRGNnQ7kohjMGu5TMyL20iX/UwCkxipWhHOhkCIB2xoz0M+CIC577w3I+lTYfbIPHeXvIVsFaiG55ZXspVrXegZNhsjddWkC3CTyLSFRb/lHTWYE9NpAxcU5C6gfo=";
    }
    else if (hostname.match("ci-mreact-4612.fxwork.kugou.com")){
        sign = "ltVkOl1oRHqJd8bbI1W6ppw37oj31lk8e47cooqefvmxxgyhXbF71JWi2pxnN8KAbpTSRRC32KLVSpRD0xIX0YJY/wucwq8NGcqVhQrqns1vg7dpVxTtH0KpUe4xXMHY2lN55hSDjl8x+qmQW1ieOECmyBuHxQb8YlIhcjwsQYk=";
    }
    else if (hostname.match("ci-mreact-4613.fxwork.kugou.com")){
        sign = "cDNOkD/6nLszlBAXu6a1p6J/MI8ngQPcJR84qKP29jdKi2kNI7h4iJxcpYI44oFoYifCROF3I7LEE8gHgWaBVdqyxcdc2K//2NVX/EErC5pt6cHDD8Epe2LqS2w/SOA3NFUs/gePjOIqRwIl61L0QakeVFr0cfUiDmIxA7rnAOg=";
    }
    //CD域名
    else if (hostname.match("cd-mreact-4661.fxwork.kugou.com")){
        sign = "FW7Mk9MH6JmtuQOP2cHVTXF5F/W632uGdEsr4x42CPTnDah4enV/nPSdTlbUEOs06MXenD2SEsmZ7OkqPLqk/TDO71QruKmuENsNHfomJdkRDdvL0dAs4B0tG0OGP6Ip4ApIZJpocUVGjl4SddJxlx/8bT/8pi5nfBxFJ2GoGd0=";
    }
    else if (hostname.match("cd-mreact-4662.fxwork.kugou.com")){
        sign = "5TIwfY+pZSGUOsD+U7dxnZgDsshXAsQa11kNN5Kzo7UIERauSym6fA7AVP0PdLuqfVoN1cwgIzfFjxxb/D53c/nfoNvmqknAd13Hp0WYGa3HoUimnsvZyO9vFLhr4pYcS8mVAUIjcFDC/JMnNL1ZoMFiPWauLRiIt/SOJr+a8MU=";
    }
    else if (hostname.match("cd-mreact-4663.fxwork.kugou.com")){
        sign = "dVM6eWsGie3NRaB26BOlLhxPFq7eNWBqTVQHlK5m8KXmdC/eeBfFDrISBti5E61YKtCBlb4bJK0g25kIceBLqT58Ih1L3Gq5JZq972E7wVqN7tmM2JnuF4Z8aqDxpUmeLVreWOInqiN279Hk5+LeJucNWL8h2A6UP+0nNJw+Vr8=";
    } else if (hostname.match("fx179.fxwork.kugou.com")) {
        sign = "MK3MVlxzpUHJWyFtUaUXbk3d9voDT1Lbj2ViXRmyDh1ir2Na19k1bE67m3bHVlRsKDXI5/1VDseB6GCBfjhG+eczERaN5YtKeUWvYqzW1TDBUTwy1wRpoFUVAYtZkQTbmk4VXwhXgKm+Pa+192eiXQUlZzpR64CMEmMOL1abS5A=";
    } else if(hostname.match("fx3071.fxwork.kugou.com")) {
        sign = "yt8Fg9T14ODGLCK7H+JWm82f8Wdq4RTZSAqyUXDYZiKC0IHwOZ3vbdqVIDMw3Tn2BmcZdmf4Togw1WiPiR0K93kRuixfyAkwPfPQX3TRBHnB4OCYEB9rOTdArBir2QG5LC8UkqnNsG6X7pj8+s1Aoz1uMRH+i8K+MM5tyUIsMI8=";
    } else if (hostname.match("fx0203.fxwork.kugou.com")) {
        sign = "FNsw8U9cB7QyWUaOpeStX8o8gWjZW0Muok4wdTUOrLH52fBnzU7DQNzm4vYbpl8qujcufH8zUC66ehlRxdJoCXueFV8qjEzWAa9Bbcy20YPcuj+i7N1pQhFvN4aOgoMndnm6weR36gACQ/9cGrXFvcudywQCY8hKSx41DiO0fVI=";
    } else if (hostname.match("fx0206.fxwork.kugou.com")) {
        sign = "v7uUPUEdGEuzoX8j105zNDfuNfWlyHB0dxgvCatYLhaQ60+k6YDRzWmZ5KuJTWovtxM3YJZ06JUv9DiUrAqUnNqiN9glDdRyoIBy7onFyHLtDwedt+dnMZDOkAKkqZD4MLLWWNErwo02PCpIWVazhuUW7mboPVhqwUaKVubx7Zg=";
    }
}

// function renderURL(url) {
//     let a = document.createElement('a');
//     a.href = url;

//     //var origin = 'http://visitor.fanxing.kugou.com';
//     let origin = 'http://' + a.host;

//     let link = origin + a.pathname.replace(/\.php/, ''),
//         act = qs.parse(a.search.substr(1)).act,
//         mtd = qs.parse(a.search.substr(1)).mtd;

//     return link + '/' + (act ? act + '.' : '') + (mtd ? mtd : '') + '/';
// }

/**
 * 跨域GET方法
 * @param {String} url 请求地址
 * @param {Object} data 传输数据
 * @param {Function} successFn 成功回调
 * @param {Function} errFn 失败回调
 */
export let getJSONP = (url, data, successFn, errFn) => {
    let jsonpCallback = data.jsonpCallback || "jsonpCallback";
    let jsonp = data.jsonp || "jsoncallback";

    delete data.jsonpCallback;
    delete data.jsonp;

    $.ajax({
        url: url,
        dataType: "jsonp",
        jsonp,
        jsonpCallback,
        cache: true,
        timeout: 30000,
        data: {
            ...data
        },
        success: function(json) {
            if ($.isFunction(successFn)) {
                successFn(json);
            }
        },
        error: function(err) {
            if ($.isFunction(errFn)) {
                errFn(err);
            }

            return (
                win.getApiFailReporter &&
                win.getApiFailReporter.push({
                    errMsg: "Get Jsonp Error",
                    jsonpUrl: url,
                    jsonpOption: (data && JSON.stringify(data)) || "no get jsonp option"
                })
            );
        }
    });
};

let imgStat = (url, _selfParmT) => {
    let img = new Image(),
        uid = utilWeb.uniqueRandomStr(8);
    _selfParmT = _selfParmT || "&_uid=";
    img.onload = img.onerror = function() {
        //销毁一些对象
        img.onload = img.onerror = null;
        img = null;
    };
    img.src = url + _selfParmT + uid;
};

/**
 * 发送统计请求
 * @method stat
 * @param {String} id 统计Id
 * @param {boolean} [async] 是否异步
 */
export let stat = (id, async) => {
    let logUrl = "https://tj.kugou.com/front/link.php?id=" + id;
    async = async || true;
    if (async) {
        //异步
        try {
            setTimeout(function() {
                imgStat(logUrl);
            }, 0);
        } catch (ex) {
            console.log(ex);
        }
    } else {
        try {
            imgStat(logUrl);
        } catch (ex) {
            console.log(ex);
        }
    }
};

/**
 *
 * @param {object} optionsObj
 * @param {(string|number)} [optionsObj.cmd]
 * @param {object} [optionsObj.data]
 * @param {string} [optionsObj.callbackName]
 * @param {string} [optionsObj.success],
 * @param {function} [optionsObj.callback]
 */
export function nativeCall(optionsObj) {
    let options = Object.assign(
        {
            cmd: "",
            data: {},
            callbackName: "",
            success: ""
        },
        optionsObj
    ); 

    if (options.callbackName && $.isFunction(options.success)) {
        //window[options.callback]
        if (!window.kgMobileCall) {
            window.kgMobileCall = {};
        }
        window.kgMobileCall[options.callbackName.split(".").pop()] = options.success;
    }
    if (boolFX) {
        if (options && options.sign) {
            sign = options.sign;
        }
        // var data = '{"cmd":"'+options.cmd+'","sign":"'+sign+'","jsonStr":'+JSON.stringify(options.data)+',"callback":"'+options.callbackName+'"}';

        let oData = {};
        oData.cmd = options.cmd;
        oData.sign = sign;
        oData.jsonStr = options.data;

        if (options.callbackName) {
            oData.callback = options.callbackName;
        } else if (options.callback) {
            const callbackname = `_fxwebviewjsfunc_${new Date().getTime()}`;
            //alert("正在设置window." + callbackname);
            window[callbackname] = (...args) => {
                options.callback(...args);
                delete window[callbackname];
            };
            oData.callback = callbackname;
        }

        let data = JSON.stringify(oData);
        if ($.os.ios) {
            // lowDebug.log( 'fanxing2://' + (options.isBanner ? "fx.activity" : "send.message") + '/?' + data );
            location.href =
                "fanxing2://" + (options.isBanner ? "fx.activity" : "send.message") + "/?" + data;
        } else if (window.external && window.external.callFanxing) {
            window.external.callFanxing(data);
            //window.external.callFanxing('{"cmd":"600","sign":"'+sign+'","jsonStr":{"pageType": 1001},"callback":""}');
        } else {
            // console.log(data)
            console.log("网络有点不给力哦!重新进入页面再点击试试吧!");
        }
    } else if ($.os.android) {
        /*if(boolKg){*/
        if (options.callbackName) {
            if (!options.data) {
                options.data = {};
            }
            options.data.callback = options.callbackName;
        }
        window.external.superCall(parseInt(options.cmd), JSON.stringify(options.data));
    } else {
        location.href =
            "kugouurl://" +
            (options.isBanner ? "fx.activity" : "start.music") +
            '/?{"cmd":"' +
            options.cmd +
            '","jsonStr":' +
            JSON.stringify(options.data) +
            ',"callback":"' +
            options.callbackName +
            '"}';
    }
}

export default {
    /**
     * 跨域GET方法
     * @param {String} url 请求地址
     * @param {Object} data 传输数据
     * @param {Function} successFn 成功回调
     * @param {Function} errFn 失败回调
     */
    getJSONP,
    stat,
    sign,
    /**
     * 与酷狗app通讯方法
     * @method nativeCall
     * @param {Object} [options] 参数
     * @param {Number} [cmd] 命令号
     * @param {Object} [data] 传输数据
     * @param {String} [callbackName] 回调方法名
     * @param {Function} [success] 回调方法
     */
    nativeCall
};
